/*
 * verify
 * selector obj
 * msg string
 * isMsg boolen 开启提示
 *
 * import Verify  from '../lib/kverify';
 passName.onchange = () => {
 Verify.name(passName, '', true);
 };
 * */

const Verify = {
    rules: {
        digits: [/^\d+$/, "请填写数字"],
        name: [/(^[\u4e00-\u9fa5]{1}[\u4e00-\u9fa5\.·。]{0,8}[\u4e00-\u9fa5]{1}$)|(^[a-zA-Z]{1}[a-zA-Z\s]{0,8}[a-zA-Z]{1}$)/, "请填写正确的姓名"],
        letters: [/^[a-z]+$/i, "请填写字母"], //纯字母
        date: [/^\d{4}-\d{2}-\d{2}$/, "请填写有效的日期，格式:yyyy-mm-dd"],
        time: [/^([01]\d|2[0-3])(:[0-5]\d){1,2}$/, "请填写有效的时间，00:00到23:59之间"],
        email: [/^[\w\+\-]+(\.[\w\+\-]+)*@[a-z\d\-]+(\.[a-z\d\-]+)*\.([a-z]{2,4})$/i, '请填写有效的邮箱'],
        url: [/^(https?|s?ftp):\/\/\S+$/i, "请填写有效的网址"],
        qq: [/^[1-9]\d{4,}$/, "请填写有效的QQ号"],
        IDcard: [/^\d{6}(19|2\d)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X|x)?$/, "请填写正确的身份证号码"],
        passport: [/^1[45][0-9]{7}|([P|p|S|s]\d{7})|([S|s|G|g]\d{8})|([Gg|Tt|Ss|Ll|Qq|Dd|Aa|Ff]\d{8})|([H|h|M|m]\d{8,10})$/, "请填写正确的护照证件号"],
        GAPass: [/^\d{6}(19|2\d)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)?$/, "请填写正确的港澳通行证证件号"],
        TWPass: [/^\d{6}(19|2\d)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)?$/, "请填写正确的台湾居民来往大陆通行证证件号"],

        tel: [/^(?:(?:0\d{2,3}[\- ]?[1-9]\d{6,7})|(?:[48]00[\- ]?[1-9]\d{6}))$/, "请填写有效的电话号码"],  //办公或家庭电话
        phone: [/^(?:[48]00[\- ]?[1-9]\d{6})$/, "请填写有效的电话号码"],  //办公或家庭电话
        mobile: [/^1[3-9]\d{9}$/, "请填写有效的手机号"],  //移动电话
        zipcode: [/^\d{6}$/, "请检查邮政编码格式"],
        chinese: [/^[\u0391-\uFFE5]+$/, "请填写中文字符"],
        username: [/^\w{3,12}$/, "请填写3-12位数字、字母、下划线"], //用户名
        password: [/^[\S]{6,16}$/, "请填写6-16位字符，不能包含空格"], //密码
        //可接受的后缀名，例如：accept(png|jpg|bmp|gif);
        accept: function (element, params) {
            if (!params) return true;
            let ext = params[0],
                value = $(element).val();
            return (ext === '*') ||
                (new RegExp(".(?:" + ext + ")$", "i")).test(value) ||
                this.renderMsg("只接受{1}后缀的文件", ext.replace(/\|/g, ','));
        }
    },
    msg: {
        0: "此处",
        fallback: "{0}格式不正确",
    },
    /*
     * 判断年龄
     * @param val
     * @param newDate
     * @param isMsg
     * @param elem
     * */
    isAgeFun: function (val, newDate, isMsg, birthdayTag, ageTypeTag) {
        let that = this;
        let r = val.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
        if (!r) return;
        newDate = newDate instanceof Date ? newDate : new Date();
        let rc = {status: true, msg: ''};
        let d = new Date(r[1], r[3] - 1, r[4]);
        if (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]) {
            let rs = newDate.getFullYear();
            let age = rs - r[1];

            if (ageTypeTag && ageTypeTag.value == 1) {
                if (age >= 12) {
                    rc = {status: true, msg: '', age: age};
                } else {
                    rc = {status: false, msg: '年龄与您选择的乘客类型不符', age: age};
                }
            } else if (ageTypeTag && ageTypeTag.value == 2) {
                if (age < 12) {
                    rc = {status: true, msg: '', age: age};
                } else {
                    rc = {status: false, msg: '年龄与您选择的乘客类型不符', age: age};
                }
            } else {
                rc = {status: true, msg: '', age: age};
            }
        } else {
            rc = {status: false, msg: '日期格式有误！'};
        }
        if (isMsg) that.isMsgFun(birthdayTag, rc);
        return rc;
    },
    name: function (elem, wmsg, isMsg) {/*名字*/
        let that = this;
        let rs = that.renderReg(elem, wmsg, 'name');
        if (isMsg) that.isMsgFun(elem, rs);
        return rs;
    },

    tel: function (elem, wmsg, isMsg) {/*电话或者手机*/
        let that = this;
        let rs = that.renderReg(elem, wmsg, 'tel');
        if (isMsg) that.isMsgFun(elem, rs);
        return rs;
    },

    phone: function (elem, wmsg, isMsg) {/*电话*/
        let that = this;
        let rs = that.renderReg(elem, wmsg, 'phone');
        if (isMsg) that.isMsgFun(elem, rs);
        return rs;
    },

    mobile: function (elem, wmsg, isMsg) {/*手机*/
        let that = this;
        let rs = that.renderReg(elem, wmsg, 'mobile');
        if (isMsg) that.isMsgFun(elem, rs);
        return rs;
    },

    chinese: function (elem, wmsg, isMsg) {/*中文*/

    },

    IDcard: function (elem, wmsg, isMsg) {/*身份证*/
        let that = this;
        let rs = that.renderReg(elem, wmsg, 'IDcard');
        if (isMsg) that.isMsgFun(elem, rs);
        return rs;
    },

    passport: function (elem, wmsg, isMsg) {/*护照*/
        let that = this;
        let rs = that.renderReg(elem, wmsg, 'passport');
        if (isMsg) that.isMsgFun(elem, rs);
        return rs;
    },

    GAPass: function (elem, wmsg, isMsg) {/*港澳通行证*/
        // console.log(elem, wmsg);
    },

    TWPass: function (elem, wmsg, isMsg) {/*台湾居民来往大陆通行证*/
        // console.log(elem, wmsg);

    },

    renderReg: function (elem, wmsg, key) {
        if (!elem && !key) return;
        let that = this;
        let rc = {status: true, msg: ''};
        let rules = that['rules'][key];

        if (elem.value.length > 10 && key === 'name') {
            rc.status = false;
            rc.msg = '名字的长度超长！';
        } else if (elem.value.length > 18 && key === 'IDcard') {
            rc.status = false;
            rc.msg = '证件号长度超长！';
        } else if (elem.value.length > 10 && key === 'passport') {
            rc.status = false;
            rc.msg = '证件号长度超长！';
        } else {
            if (rules[0].test(elem.value)) {
                rc.status = true;
            } else {
                rc.msg = wmsg ? wmsg : rules[1];
                rc.status = false;
            }
        }
        return rc;
    },

    renderMsg: function () {

    },

    isMsgFun: function (elem, obj) { /*提交表单*/
        let that = this;
        if (!elem && !obj) return;
        let box = elem.parentNode;
        if (obj.status) {
            that.reChild(box);
        } else {
            that.reChild(box);
            let rc = document.createElement('span');
            rc.className = "item-input-msg";
            rc.innerHTML = `<span class="weui_icon_cancel"></span>${obj.msg}`;
            box.appendChild(rc);
        }
    },
    reChild: function (box) {
        if (!box) return;
        let child = box.childNodes || [];
        for (let i = 0; i < child.length; i++) {
            if (child[i].className === 'item-input-msg') {
                box.removeChild(child[i]);
            }
        }
    }
};

function formatBirthyDay(val) {
    let rc = "";
    if (val.length == 15) {
        rc = "19" + val.substr(6, 6);
    } else if (val.length == 18) {
        rc = val.substr(6, 8);
    }
    rc = rc.replace(/(.{4})(.{2})/, "$1-$2-");
    return rc;
}

export {Verify, formatBirthyDay};
